<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>大奔农庄</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>    
    <style>
    	.none{display:none;}
    </style>
</head>
<body>
	<div class="aui-content aui-margin-b-15">
		<form id="phoneForm">
		    <ul class="aui-list aui-form-list">
		        <li class="aui-list-item" id="phone_box">
		            <div class="aui-list-item-inner">
		                <div class="aui-list-item-input">
		                    <input type="text" placeholder="手机号" id="phone" name="phone">
		                </div>
		            </div>
		        </li> 
		        <p class="height15"></p>
		        <li class="aui-list-item">
		            <div class="aui-list-item-inner">
		                <div class="aui-list-item-input">
		                    <input type="text" placeholder="验证码" id="verify" name="verify">
		                </div>
		                <div class="aui-pull-right yanzhengma" onclick="sendSMS(this,event);" style="max-width:125px;">获取验证码</div>
		            </div>
		        </li> <p class="height15"></p>
		     </ul>
		 </form>    
     </div>
     <div class="aui-content-padded">
		 <div class="aui-btn aui-btn-block aui-btn-success aui-btn-sm" id="upphone_yz" onclick="phoneYanzheng();">验证身份</div>
		 <div class="aui-btn aui-btn-block aui-btn-success aui-btn-sm none"id="upphone_xg" onclick="phoneChange();">确认修改</div>
	 </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script>
var db_key = 'user',db_user,myfrom;
apiready = function() {
    db_user = $api.getStorage(db_key);//获取用户信息
    myfrom = $api.byId('phoneForm');
}
//验证身份
function phoneYanzheng() {
	$verify = $api.byId("verify");//短信验证码
	$phone = $api.byId("phone");//手机号码

    if ($verify.value =="") {
        api.toast({msg: '短信验证码不能为空！'});
        return false;
    }
    
    api.showProgress();
    var useParam = {
    	rtk : RSapi.myrtk,
        mytoken : db_user.mytoken,
        phone	: $phone.value, 
        verify	: $verify.value, 
    };
	//修改登录密码
	api.ajax({
        url: RSapi.base+RSapi.yanzhengPhone,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: {values: useParam},
    },function(ret, err){ 
        if (err) { //alert(err.msg);return false;
            api.toast({msg:err.msg});
        }else if (ret.status) { 
        	//验证成功
            api.alert({msg:ret.info});
            //清空手机号码和验证码
            $api.val($api.byId('phone'), '');
            $api.val($api.byId('verify'), '');
            //隐藏验证按钮，显示修改按钮
            $api.addCls($api.byId('upphone_yz'), 'none');
            $api.removeCls($api.byId('upphone_xg'), 'none');
        }else { 
            api.toast({msg:ret.info});
        }
        api.hideProgress();
    });
}
//验证手机号码
function phoneChange() {
	$verify = $api.byId("verify");//短信验证码
	$phone = $api.byId("phone");//手机号码

	if ($phone.value =="") {
        api.toast({msg: '手机号码不能为空！'});
        return false;
    }
    if ($verify.value =="") {
        api.toast({msg: '短信验证码不能为空！'});
        return false;
    }
    
    api.showProgress();
    var useParam = {
    	rtk : RSapi.myrtk,
        mytoken : db_user.mytoken,
        phone	: $phone.value, 
        verify	: $verify.value, 
    };
	//修改登录密码
	api.ajax({
        url: RSapi.base+RSapi.changePhone,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: {values: useParam},
    },function(ret, err){ 
        if (err) { //alert(err.msg);return false;
            api.toast({msg:err.msg});
        }else if (ret.status) { 
        	//绑定成功
            api.alert({msg:ret.info});
            api.closeWin();
        }else { 
            api.toast({msg:ret.info});
        }
        api.hideProgress();
    });
}
/*发送验证码*/
function sendSMS(obj,e) {
	e.preventDefault();
		var preg = new RegExp(/1\d{10}/);
		if(preg.test(myfrom.phone.value)) {
			//倒计时开始
			$api.attr(obj,'disabled','disabled');
			var wait = 60;
			obj.innerHTML = "重新发送（"+wait+"）";
			var interval = setInterval(function(){
				wait = --wait;
				if(wait <= 0) {
					obj.innerHTML = "发送验证码";
					$api.removeAttr(obj,'disabled');
					clearInterval(interval);
				}else {
					obj.innerHTML = "重新发送（"+wait+"）";
				}
			}, 1000);
			// 发送验证码
			api.ajax({
				url: RSapi.base+RSapi.Mverify,
				method: 'post',
				cache: false,
				timeout: 30,
				dataType: 'json',
				data: { values: {rtk:RSapi.myrtk, phone:myfrom.phone.value, actstr: '修改手机号码', check:1} },//确保用户不存在
			}, function(ret, err){
				if(err){//请求失败
					wait=1;
					//alert(JSON.stringify(err));
					api.alert({msg: JSON.stringify(err)});
				}else {
					if (ret.status) {
						//成功了
						verify_code = ret.info.code;
						verify_phone = ret.info.phone;
						api.toast({msg:'短信验已发送成功，请注意查收！'});
					}else {
						//失败了
						wait=1;
						api.toast({msg: ret.info});
					}
				}
			});
		}else {
			api.toast({msg: "请输入正确的手机号码"});
		}
}
</script>
</html>